package com.youfang.sixthmedia.message.center;

import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.youfang.sixthmedia.controller.HeaderController;
import com.youfang.sixthmedia.message.model.Buddy;
import com.youfang.sixthmedia.net.Response;

public class GetBuddyListResponse extends Response {

	private List<Buddy> dataList = new LinkedList<Buddy>();

	@Override
	protected void decodeBody(JSONObject bodyJSON) throws Exception {

		JSONArray arr = bodyJSON.getJSONArray("data");
		for (int i = 0; i < arr.length(); i++) {
			JSONObject o = arr.getJSONObject(i);
			dataList.add(process(o));

		}

	}

	private Buddy process(JSONObject o) throws Exception {

		Buddy item = new Buddy();

		item.distance = getDouble(o,"distance");
		
		item.longitude =getDouble(o,"longitude");
		item.latitude = getDouble(o,"latitude");
		item.userid = o.getInt("userid");
		item.username = o.getString("username");
		item.nickname = o.getString("nickname");
		item.introduce = o.getString("introduce");
		item.sex = o.getInt("sex");
		item.mobile = o.getString("mobile");

		// change logourl to logo
		item.logo = o.getString("logourl");
		item.email = o.getString("email");

		// use userid to be innerID
		item.innerID = item.userid;
		item.logoBitmap = HeaderController.Cache.getFromCache(item);

		return item;

	}

	public List<Buddy> getDataList() {
		return dataList;
	}

	public void setDataList(List<Buddy> dataList) {
		this.dataList = dataList;
	}

}
